/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telas;

import controle.BancoDeDados;
import controle.ControladorCadastro;
import controle.JTextFieldLimit;
import enumerations.AcomodacaoEstado;
import exceptions.SemAcomodacaoLivreException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import modelo.Acomodacao;
import modelo.Animal;
import modelo.Estadia;

/**
 *
 * @author eduardo
 */
public class TelaCheckin extends javax.swing.JFrame {

    /**
     * Creates new form TelaCheckin
     */
    public TelaCheckin() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxAnimal = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxAcomodacao = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldPrevisaoSaida = new javax.swing.JTextField();
        jTextFieldPrevisaoSaida.setDocument(new JTextFieldLimit(2));
        jLabel5 = new javax.swing.JLabel();
        jButtonConfirmarCheckin = new javax.swing.JButton();
        jTextFieldEntrada = new javax.swing.JTextField();
        jTextFieldPrevisaoSaida.setDocument(new JTextFieldLimit(2));

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Check-in");

        jLabel1.setText("Animal:");

        jComboBoxAnimal.setModel(new javax.swing.DefaultComboBoxModel(BancoDeDados.getInstance().getAnimais().toArray()));
        jComboBoxAnimal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxAnimalActionPerformed(evt);
            }
        });

        jLabel2.setText("Acomodação:");

        jLabel3.setText("Entrada:");

        jLabel4.setText("Previsão de saída em:");

        jLabel5.setText("dias");

        jButtonConfirmarCheckin.setText("Confirmar");
        jButtonConfirmarCheckin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmarCheckinActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(51, 51, 51)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel4)
                    .add(jLabel3)
                    .add(jLabel2)
                    .add(jLabel1))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jComboBoxAcomodacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(jTextFieldPrevisaoSaida, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel5))
                    .add(jComboBoxAnimal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTextFieldEntrada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(61, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButtonConfirmarCheckin)
                .add(145, 145, 145))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(50, 50, 50)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jComboBoxAnimal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jComboBoxAcomodacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextFieldEntrada, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextFieldPrevisaoSaida, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 39, Short.MAX_VALUE)
                .add(jButtonConfirmarCheckin)
                .add(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxAnimalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxAnimalActionPerformed
        // TODO add your handling code here:
        Animal animalSelecionado = (Animal) jComboBoxAnimal.getSelectedItem();
        try {
            jComboBoxAcomodacao.setModel(new DefaultComboBoxModel(ControladorCadastro.listarAcomodacoesAdequadas(animalSelecionado)));
        } catch (SemAcomodacaoLivreException e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }//GEN-LAST:event_jComboBoxAnimalActionPerformed

    private void jButtonConfirmarCheckinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmarCheckinActionPerformed
        // TODO add your handling code here:

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            Date dataDeEntrada = sdf.parse(jTextFieldEntrada.getText());
            Acomodacao acomodacao = (Acomodacao) jComboBoxAcomodacao.getSelectedItem();
            acomodacao.setEstado(AcomodacaoEstado.OCUPADO);
            Animal animal = (Animal) jComboBoxAnimal.getSelectedItem();
            int qtdeDiasPrevisto = Integer.parseInt(jTextFieldPrevisaoSaida.getText());
            Estadia estadia = new Estadia(dataDeEntrada, acomodacao, animal, qtdeDiasPrevisto);

            ControladorCadastro.cadastrar(estadia);


            File arquivoEtiqueta = new File("etiqueta.txt");
            FileOutputStream fos = new FileOutputStream(arquivoEtiqueta);
            String etiqueta = estadia.imprimirEtiqueta();
            fos.write(etiqueta.getBytes());
            fos.close();

            JOptionPane.showMessageDialog(this, "Check-in realizado com sucesso.\n\n"
                    + "------------------------------\n"
                    + estadia.imprimirEtiqueta()
                    + "\n------------------------------");
        } catch (ParseException ex) {
            JOptionPane.showMessageDialog(this, "Preencha os campos com os tipos de dados adequados.");
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(this, "Preencha todos os campos.");
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_jButtonConfirmarCheckinActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaCheckin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaCheckin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaCheckin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaCheckin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaCheckin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirmarCheckin;
    private javax.swing.JComboBox jComboBoxAcomodacao;
    private javax.swing.JComboBox jComboBoxAnimal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextFieldEntrada;
    private javax.swing.JTextField jTextFieldPrevisaoSaida;
    // End of variables declaration//GEN-END:variables
}
